import request from "@/utils/request";
import { getRefreshToken } from "@/utils/token";

export const userRegisterService = (data) =>
  request.post("/users/register", data);

export const userLoginService = ({ repassword, ...rest }) =>
  request.post("users/login", rest);

export const reqLogout = () => request.post("/users/logout");

export const getCaptcha = () => request.get("/users/captcha");
// 发送邮箱验证码
export const sendEmailCode = (data) => request.post("/users/emailcode", data);
export const userGetInfoService = () => request.get("/users/info");
export const userUpdateInfoService = ({ id, nickname, email }) =>
  request.put("/my/userinfo", { id, nickname, email });
export const userUpdatePassService = ({ old_pwd, new_pwd, re_pwd }) =>
  request.patch("/my/updatepwd", { old_pwd, new_pwd, re_pwd });
export const userUploadAvatarService = (avatar) =>
  request.patch("/my/update/avatar", { avatar });
export const verifyEmail = (data) => request.post("/users/verifyemail", data);
export const resetPassword = (data) =>
  request.post("/users/forgetPassword", data);

export const getUserListService = (params) =>
  request.get("/users/list", { params });
export const reqAddOrUpdate = (data) => {
  if (data.id) {
    return request.put("/users/update", data);
  } else {
    return request.post("/users/save", data);
  }
};
export const reqRemoveUser = (id) => request.delete(`/users/delete/${id}`);
export const reqChangeUserStatus = (id, data) =>
  request.patch(`/users/state/${id}`, data);
export const reqAllRole = (id) => request.get(`/roles/toAssign/${id}`);
export const reqSetUserRole = (data) => request.post("/users/role", data);
export const refreshToken = (data) =>
  request.post("/users/refreshToken", {
    refreshToken: getRefreshToken(),
  });
